package cn.ittaoli.szdz.controller;

import cn.ittaoli.szdz.model.DzSetting;
import cn.ittaoli.szdz.service.DzSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by lub_lenovo on 2017/8/17.
 */
@Controller
@RequestMapping("/setting")
public class SettingController {
    @Autowired
    private DzSettingService dzSettingService;

    @RequestMapping("/index")
    public String index(Model model){
        List<DzSetting> dzSettings=dzSettingService.selectConfigInfo();
        model.addAttribute("menu","配置信息");

        model.addAttribute("settings",dzSettings);
        return "/setting/index";
    }


    @RequestMapping("/addConfig")
    public String addConfig(Model model){
        model.addAttribute("menu","配置信息");

        return "/setting/addConfig";
    }

    @RequestMapping("/doAddConfig")
    public String doAddConfig(DzSetting dzSetting){
        Integer res=dzSettingService.insert(dzSetting);
        if(res>0){
            return "redirect:/setting/index";
        }
        return "addConfig";
    }

    @RequestMapping("/editConfig")
    public String editConfig(String k,Model model){
        DzSetting dzSetting=dzSettingService.selectOne(k);
        model.addAttribute("setting",dzSetting);
        model.addAttribute("menu","配置信息");

        return "/setting/editConfig";
    }

    @RequestMapping("/doEditConfig")
    public String doEditConfig(DzSetting dzSetting){
        Integer res= dzSettingService.update(dzSetting);
        if(res>0){
            return "redirect:/setting/index";
        }
        return "editConfig";
    }

    @RequestMapping("/delConfig")
    @ResponseBody
    public Integer delConfig(String k){
        return dzSettingService.delete(k);
    }


    @RequestMapping("/editSysConfig")
    public String editSysConfig(Model model){
        List<DzSetting> dzSettings=dzSettingService.selectSysConfigInfo();
        model.addAttribute("sysSettings",dzSettings);
        model.addAttribute("menu","系统设置");

        return "/setting/editSysConfig";
    }

    @RequestMapping("/doEditSysConfig")
    @ResponseBody
    public Integer doEditSysConfig(String obj){
        return dzSettingService.updateSysConfig(obj);
    }
}
